package com.gegong.vmware.workstation.autoconfig;

import com.gegong.vmware.workstation.VMwareWorkstationManagementProperties;
import com.gegong.vmware.workstation.VmwareRestApiServerProperties;
import com.gegong.vmware.workstation.service.VMManagementService;
import com.gegong.vmware.workstation.service.VMNetworkAdaptersManagementService;
import com.gegong.vmware.workstation.service.VMPowerManagementService;
import com.gegong.vmware.workstation.service.VMwareWorkstationManagementService;
import com.gegong.vmware.workstation.service.impl.DefaultVMManagementServiceImpl;
import com.gegong.vmware.workstation.service.impl.DefaultVMNetworkAdaptersManagementServiceImpl;
import com.gegong.vmware.workstation.service.impl.DefaultVMPowerManagementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(VMwareWorkstationManagementService.class)
@EnableConfigurationProperties(VmwareRestApiServerProperties.class)
public class VmwareRestApiAutoConfigure {


    @Autowired
    public VmwareRestApiAutoConfigure(VmwareRestApiServerProperties properties) {
        VMwareWorkstationManagementProperties.restApiRootUrl = String.format("%s://%s:%d/api", properties.getProtocol(), properties.getHost(), properties.getPort());
        VMwareWorkstationManagementProperties.username = properties.getUsername();
        VMwareWorkstationManagementProperties.password = properties.getPassword();
    }

    @Bean
    @ConditionalOnMissingBean(VMManagementService.class)
    VMManagementService vmManagementService() {
        return new DefaultVMManagementServiceImpl();
    }


    @Bean
    @ConditionalOnMissingBean(VMPowerManagementService.class)
    VMPowerManagementService vmPowerManagementService() {
        return new DefaultVMPowerManagementServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(VMNetworkAdaptersManagementService.class)
    VMNetworkAdaptersManagementService vmNetworkAdaptersManagementService(VMManagementService vmManagementService,
                                                                          VMPowerManagementService vmPowerManagementService) {
        return new DefaultVMNetworkAdaptersManagementServiceImpl(vmManagementService, vmPowerManagementService);
    }
}
